﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using verk2013_project_group12.Filters;
using verk2013_project_group12.Models;
using WebMatrix.WebData;

namespace verk2013_project_group12.Controllers
{
    public class HomeController : Controller
    {
        private IAppRepository _repository = null;

        public HomeController()
        {
            _repository = new AppRepository();
        }

        public HomeController(IAppRepository rep)
        {
            _repository = rep;
        }


        [InitializeSimpleMembership]
        public ActionResult MyProfile()
        { 
            // This function displays your profile as other user will see it.
            var userId = WebSecurity.GetUserId(User.Identity.Name);
            return RedirectToAction("Profile", new { id = userId});
        }

        public ActionResult Index()
        { 
            // This function selects 3 latest news and returns them.
            var topNews = from i in _repository.AllNews
                         .OrderByDescending(i => i.ID)
                         .Take(3)
                          select i;
           

            return View(topNews);
        }

        public ActionResult About()
        {
            // This function returns all FAQs
            FAQRepository FAQ = new FAQRepository();
            var getFAQ = FAQ.GetAllFAQs();

            return View(getFAQ);
        }

        public ActionResult _PlayerList()
        { 
            // This function is for the partial view.
            UserRepository users = new UserRepository();
            var fiveUsers = users.GetAllUsers().Take(5);
            // Returns oldest 5 users.
            return PartialView(fiveUsers);
        }

        
        public ActionResult PlayerList()
        {
            if (Roles.IsUserInRole(WebSecurity.CurrentUserName, "Admin"))
            {
                return RedirectToAction("PlayerList", "Admin");
                //If loggedin user is an admin he will be redirected to the playerlist view for the admin.
            }
    
            var allUsers = _repository.AllUsers;
            return View(allUsers);
        }

        public ActionResult _Leaderboard()
        { 
            // This Function returns the top five user orderd by score.
            UserRepository users = new UserRepository();
            var topFive = (from u in users.GetAllUsers()
                           orderby u.OverallScore descending
                           select u).Take(5);
                          
                           
            return PartialView(topFive);
        }

        public ActionResult Profile(int? id)
        {
            // This function returns the profile of the user with id of id
            if (id != null)
            {
                UserRepository users = new UserRepository();
                var user = users.GetUserById(id.Value);
                if (user != null)
                {
                    return View(user);
                }
                else
                {
                    return View("Error");
                }
            }
            else
            {
                return View("Error");
            }
        }
    }
}